package mlz.c_udp2;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class Server {
    public static void main(String[] args) throws IOException {
        System.out.println("---------服务器启动------");
        //1.创建服务器对象 -> 端口号 -> 6666
        DatagramSocket socket = new DatagramSocket(6666);

        while (true) {
            //2.创建一个数据包对象,用于接受数据包
            byte[] bytes = new byte[1024 * 64];// 64kb
            DatagramPacket packet = new DatagramPacket(bytes,bytes.length);
            System.out.println(packet);


            //3.开始做真正的数据接收任务
            socket.receive(packet);

            //获取本次数据包接受了多少的数据
            int length = packet.getLength();

            String rs = new String(bytes, 0, length);
            System.out.println(rs);

            //输出对应的客户端的ip
            System.out.println(packet.getAddress().getHostAddress());
            System.out.println(packet.getPort());

            System.out.println("-----------------------------");
        }

        //释放资源
        //socket.close();

    }

}











